package com.blueberry.commbank.domain.entity;

import com.blueberry.commbank.exception.DailyLimitExceededException;
import com.blueberry.commbank.exception.InsufficientFundsException;
import com.blueberry.commbank.exception.InvalidCurrencyException;
import java.util.Currency;

public class Account {
    private Integer id;
    private String accountId;
    private String accountNumber;
    private String pinNumber;
    private Money balance;
    private Money dailyLimit;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public String getPinNumber() {
        return pinNumber;
    }

    public void setPinNumber(String pinNumber) {
        this.pinNumber = pinNumber;
    }

    public Money getBalance() {
        return balance;
    }

    public void setBalance(Money balance) {
        this.balance = balance;
    }

    public Money getDailyLimit() {
        return dailyLimit;
    }

    public void setDailyLimit(Money dailyLimit) {
        this.dailyLimit = dailyLimit;
    }

    public Currency getCurrency() {
        return balance.getCurrency();
    }

    public void deposit(Money money) {
        if (!money.getCurrency().equals(this.getCurrency()))
            throw new InvalidCurrencyException("The currency of the deposit diffs from the account currency");
        balance = balance.add(money);
    }

    public void withdraw(Money money) {
        if (!money.getCurrency().equals(this.getCurrency()))
            throw new InvalidCurrencyException("The currency of the deposit diffs from the account currency");
        if (balance.compareTo(money) < 0)
            throw new InsufficientFundsException("Not enough funds");
        if (dailyLimit.compareTo(money) < 0)
            throw new DailyLimitExceededException("Exceed max daily limit");
        balance = balance.subtract(money);
    }
}
